#include "adc_proc.h"
#include <string.h>

adc_data_t adc_raw = {0};
adc_data_t adc_proc = {0};

void ADC_PROC_Start()
{
    HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
    HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_raw.data, ADC_CHANNEL_NUM);
}

void ADC_RecursiveMeanFilter()
{
#define FILTER_SIZE 10
    static adc_data_t filter[FILTER_SIZE] = {0};
    static uint8_t index = 0;

    memcpy(&filter[index++], adc_raw.data, sizeof(adc_data_t));
    if (index >= FILTER_SIZE)
        index = 0;

    for (uint8_t i = 0; i < ADC_CHANNEL_NUM; i++)
    {
        uint32_t sum = 0;
        for (uint8_t j = 0; j < FILTER_SIZE; j++)
            sum += filter[j].data[i];
        adc_proc.data[i] = sum / FILTER_SIZE;
    }
}
